perm filename A90.TEX[106,RWF]1 blob
sn#807794 filedate 1985-10-09 generic text, type C, neo UTF8
COMMENT ⊗ VALID 00002 PAGES
C REC PAGE DESCRIPTION
C00001 00001
C00002 00002 \magnification\magstephalf
C00014 ENDMK
C⊗;
\magnification\magstephalf
\input macro.tex
\def\today{\ifcase\month\or
January\or February\or March\or April\or May\or June\or
July\or August\or September\or October\or November\or December\fi
\space\number\day, \number\year}
\baselineskip 14pt
\rm
\font\rmn=amr9
\line{\sevenrm a90.tex[106,phy] \today\hfill}
\bigskip
\line{\bf Types in General\hfil}
We have encountered several {\it types\/} of values in Pascal: the integers,
the real numbers, the characters, the Boolean (truth) values. Programs may
contain variables of those types, each allowed to have any value of its type.
The classification of variables by type has a common-sense basis; if $X$ is
a real number and $C$ is a character, it makes sense to talk about the
square root of~$X$ and the next character after~$C$, but not the square
root of~$C$ and the next number after~$X$.
There are other types in Pascal, in fact an infinite number of them. There
is, however, a short list of ways a type can be constructed. In order to talk
intelligibly about the rules that apply to all types, we briefly present here
all the rules for constructing types, with a minimum of explanation of what
they are for.
\medskip
(1) The {\it primitive\/} types are the real numbers, the integers, the
characters, and the truth values, known respectively as {\tt REAL},
{\tt INTEGER}, {\tt CHAR}, and {\tt BOOLEAN}.
(2) If ${\cal I}↓0,{\cal I}↓1,\ldots,{\cal I}↓n$ are identifiers not
otherwise used, then $({\cal I}↓0,{\cal I}↓1,\ldots,{\cal I}↓n)$ is a new
type, called an enumerated type; it contains the distinct values
${\cal I}↓0<{\cal I}↓1<{\cal I}↓2<\cdots <{\cal I}↓n$.
(3) If $\cal T$ is an ordinal type, and $a$, $b$ are values of that type,
then the subset of~$\cal T$ consisting of the values between~$a$ and~$b$
inclusive is a type, known as $a\ldt b$; examples are $-5\ldt 20$
(integers in the range from~$-5$ to~$+20$) and '$A$'$\ldt$'$Z$' (capital
letters, and any other characters ordered between $A$ and~$Z$). Such a
type is called a {\it subrange\/} type. Variables of a subrange type have
values that also belong to the {\it host type\/} to which $a$ and~$b$ belong.
That is, if $I$ is a variable of type $-5\ldt 20$, the value of~$I$ can be
used whenever an integer is needed. However, only those values of the
host type that fall between $-5$ and~20 may be assigned to~$I$.
(4) If ${\cal T}↓1$ is an ordinal type, and ${\cal T}↓2$ is any type, then
there is a type consisting of a sequence of values from~${\cal T}↓2$, one
for each value of ${\cal T}↓1$. This type, called an {\it array\/} type,
is known as
${\tt ARRAY}[{\cal T}↓1]$ of~${\cal T}↓2$. For example, if ${\cal T}↓1$
is '$A$'$\ldt$'$D$', and ${\cal T}↓2$ is {\tt INTEGER}, the type
{\tt ARRAY['$A$'$\ldt$'$D$']} is made up of sequences of four integers, like
$(7,-3,7,46)$, and the positions in the sequence, called {\it indices\/},
are the values of~${\cal T}↓1$. In this case, the array has~7 in the
positions with index~'$A$' and~'$C$', $-3$ with index '$B$', and 46
with index '$D$'. This array may be thought of as a table like
$$\raise12pt\vtop{\offinterlineskip%
\halign{\strut#&\vrule#&\quad\hfil#\hfil\quad&\vrule#&\quad\hfil#\hfil\quad%
&\vrule#&\quad\hfil#\hfil\quad%
&\vrule#&\quad\hfil#\hfil\quad&\vrule#\cr
\multispan9\hrulefill\cr
\omit&height2pt&\omit&&\omit&&\omit&&\omit&\cr
&&$A$&&$B$&&$C$&&$D$&\cr
\omit&height2pt&\omit&&\omit&&\omit&&\omit&\cr
\multispan9\hrulefill\cr
\omit&height2pt&\omit&&\omit&&\omit&&\omit&\cr
&&7&&$-3$&&7&&46&\cr
\omit&height2pt&\omit&&\omit&&\omit&&\omit&\cr
\multispan9\hrulefill\cr
}}
\qquad
\hbox{or}
\qquad
\raise12pt\vtop{\offinterlineskip%
\halign{\strut#&\vrule#&\quad\hfil#\hfil\quad&\vrule#%
&\quad \hfil#\quad&\vrule#\cr
\multispan5\hrulefill\cr
\omit&height2pt&\omit&&\omit&\cr
&&$A$&&7&\cr
\omit&height2pt&\omit&&\omit&\cr
\multispan5\hrulefill\cr
\omit&height2pt&\omit&&\omit&\cr
&&$B$&&$-3$&\cr
\omit&height2pt&\omit&&\omit&\cr
\multispan5\hrulefill\cr
\omit&height2pt&\omit&&\omit&\cr
&&$C$&&$7$&\cr
\omit&height2pt&\omit&&\omit&\cr
\multispan5\hrulefill\cr
\omit&height2pt&\omit&&\omit&\cr
&&$D$&&$46$&\cr
\omit&height2pt&\omit&&\omit&\cr
\multispan5\hrulefill\cr
}}$$
where the letters are fixed but the numbers may be changed.
(5) If ${\cal T}↓1,{\cal T}↓2,\ldots,{\cal T}↓n$ are types and
${\cal I}↓1,{\cal I}↓2,\ldots,{\cal I}↓n$ are identifiers, then
{\tt RECORD} ${\cal I}↓1$: ${\cal T}↓1$; ${\cal I}↓2$: ${\cal T}↓2$;
$\ldots$; ${\cal I}↓n$; ${\cal T}↓n$ {\tt END} is a
type consisting of sequences $(x↓1,x↓2,\ldots,x↓n)$,
where each~$x↓i$ belongs to the corresponding type~${\cal T}↓i$. For example,
the type
{\obeylines
\obeyspaces
\let =\
\tt
RECORD(HOUR: 1..24,MINUTE: 0..59)
}
\noindent
has values which can represent times on a 24-hour clock.
(6) If ${\cal T}$ is a type, then {\tt SET OF} ${\cal T}$ is a type.
(7) If ${\cal T}$ is a type not consisting of files, then {\tt FILE OF} ${\cal T}$
is a type.
(8) If ${\cal I}$ is an identifier (of a record type), then $\uparrow{\cal I}$
is a type, consisting of pointers to records.
Summing up, the general form of a type is
\figbox 7truein:
\vfill\eject
Between the constant definition part and the variable declaration part of
a block, there may be a type definition part giving names
(identifiers) to types. For example, here is a valid block head:
\smallskip
{\obeylines\obeyspaces\let =\ \tt
CONST SIZE=20;
TYPE DIGIT=0$\ldt$9;
DATTABLE=ARRAY[1$\ldt$SIZE] OF REAL;
VAR I: INTEGER;
OLDDATA,NEWDATA: DATTABLE;
FUNCTION F(D: DIGIT): CHAR;
BEGIN
F:=CHR(ORD('0')+D)
END;
}
\smallskip
The general form of the type definition part of a block head is:
\figbox 1.6truein:
\bigskip\noindent
If a program requires the use of a non-primitive type, there appears to
be no good reason not to give it a name by a type definition, and thereafter
refer to it only by that name. Doing this avoids several seductive
pitfalls. For details, see Standard Pascal, pp.\ ---.
\bigskip
\line{\copyright 1985 Robert W. Floyd\hfill}
\line{First draft (not published) October 1, 1985;\hfil}
%revised: October 4, 1985\hfil}
%subsequently revised.\hfill}
\bye